/**
 * 
 */
package com.xbang.attendance.web.action.purchase.relateddevliery;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.PurchaseDetails;
import com.xbang.attendance.entity.order.PurchaseOrder;
import com.xbang.attendance.service.ProductService;
import com.xbang.attendance.service.PurchaseOrderService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 6137752167041062321L;

	private static final Log logger = LogFactory.getLog(Index.class);

	@Autowired
	private ProductService productService;
	@Autowired
	private PurchaseOrderService purchaseOrderService;

	private List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();

	private Integer id;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Action("/purchase/relateddevliery/list")
	public String list() throws Exception {
		
		String sqlSentence = "FROM PurchaseDetails purchasedetails WHERE 1 = 1 ";
		searchComponent.addSearchElement("gt.purchasedetails.balanceNumber", new BigDecimal(0));
		List<PurchaseDetails> tmpPurchaseDetails = purchaseOrderService.searchForList(sqlSentence, searchComponent);
		
		Set<Integer> ids = new HashSet<Integer>();
		for (PurchaseDetails purchaseDetails : tmpPurchaseDetails) {
			ids.add(purchaseDetails.getPurchaseOrder().getId());
		}

		sqlSentence = " FROM PurchaseOrder purchaseorder  WHERE 1 = 1 and id in (:ids)";

		searchComponent.resetWhereSentence();
//		searchComponent.addSearchElement("in.purchaseorder.id", ids);
		Map<String , Object> parameters = new HashMap<String, Object>();
		parameters.put("ids", ids);
		// logger.info(">>>>>>>>>>>>>>>>>");
		Result result = purchaseOrderService.searchByPage(sqlSentence, parameters, page);

		// logger.info(">>>>>>>>>>>>>>>>>");
		purchaseOrders = result.getItems();
		page = result.getPage();

		return super.execute();
	}

	@Override
	public void resetCustomerSearchCriteria() {
	}

	// @Action("/purchase/related/${id}")
	public String addRelated() {
		System.out.println(id);
		return "add";
	}

	public List<PurchaseOrder> getPurchaseOrders() {
		return purchaseOrders;
	}

	public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
		this.purchaseOrders = purchaseOrders;
	}

}
